//vue
const app = new BaseValidateVue({
    el: '#app',
    data() {
        return {
            formData: {
                idcId: '',
                houseNumber: '',
                houseName: '',
                houseType: '1',
                houseProvince: '',
                houseCity: '',
                houseCounty: '',
                houseAddress: '',
                zipCode: '',
                houseOfficer: '',
                reportStatus: '2',
                defaultHouse: '0',
                houseAreas: [
                    {
                        areaNumber: '',
                        areaName: '',
                        rackNumber: '',
                    },
                ],
                houseLinkInfos: [
                    {
                        bandWidth: '',
                        gatewayIp: '',
                        linkType: 1,
                        accessUnit: '',
                    },
                ],
            },
            provinces: [],
            cities: [],
            counties: [],
            operList: [],
            houseOfficerList: [],
            provinceCityArea: {
                provinceMap: new Map(),
                cityMap: new Map(),
                areaMap: new Map(),
            },
            ruleList: [
                new ValidateObject({
                    ruleName: 'house_number_validate',
                    validate: this.houseNumberValidate,
                }),
                new ValidateObject({
                    ruleName: 'area_number_validate',
                    validate: this.areaNumberValidate,
                }),
                new ValidateObject({
                    ruleName: 'house_name_validate',
                    validate: this.houseNameValidate,
                }),

            ],
        }
    },
    methods: {
        checkId({id = null}) {
            return id !== null;
        },
        createValidateList() {
            return this.ruleList;
        },
        addHouseArea() {
            this.formData.houseAreas.push({
                areaNumber: '',
                areaName: '',
                rackNumber: '',
                show: true,
            })
        },
        addHouseLinkInfo() {
            this.formData.houseLinkInfos.push({
                bandWidth: '',
                gatewayIp: '',
                linkType: '1',
                accessUnit: '',
                show: true,
            })
        },
        deleteHouseArea(index) {
            this.formData.houseAreas.splice(index, 1)
        },
        deleteHouseLinkInfo(index) {
            if (this.formData.houseLinkInfos.length === 1) {
                rxPrompt.msg(`必须保留一个`)
            } else {
                this.formData.houseLinkInfos.splice(index, 1)
            }
        },
        submitForm() {
            this.$validator.validateAll().then(async (res) => {
                if (res) {
                    const result = await baseHouseInfoApi.add(this.formData)
                    if (result.code === 200) {
                        await rxPrompt.msg('新增成功')
                        forward('/basic-data/admin_house_info')
                    } else {
                        await rxPrompt.msg(`新增失败,${result.msg}`)
                    }
                } else {
                    rxPrompt.dangerMsg("输入存在错误！");
                }
            })
        },
        async initHouseProvinceData() {
            this.provinces = await sysConfProvinceApi.selectList()
        },
        async houseNumberValidate(val, args) {
            if (this.formData) {
                const {idcId = ''} = this.formData;
                if (idcId === '' || val === '') {
                    return true;
                }
                return await baseHouseInfoApi.selectHouseNumByIdcId(idcId, val);
            }
        },
        async houseNameValidate(val, args) {
            const {idcId = ''} = this.formData;
            if (idcId === '' || val === '') {
                return true;
            }
            return await baseHouseInfoApi.selectHouseNameByIdcId(idcId, val);
        },
        async areaNumberValidate(val, args) {
            const areaNums = this.formData.houseAreas.map(obj => obj.areaNumber);
            let flagArray = [];
            for (var i = 0; i < areaNums.length; i++) {
                if (flagArray[areaNums[i]]) {
                    return false;
                }
                flagArray[areaNums[i]] = true;
            }
            return true;
        },
    },
    watch: {
        ['formData.houseProvince']() {
            this.formData.houseCity = ''
        },
        ['formData.houseCity']() {
            this.formData.houseCounty = ''
        }
    },
    computed: {
        provinceMapComputed() {
            return this.provinceCityArea.provinceMap
        },
        cityMapComputed() {
            return this.provinceCityArea.cityMap.get(this.formData.houseProvince) || new Map()
        },
        areaMapComputed() {
            return this.provinceCityArea.areaMap.get(this.formData.houseCity) || new Map()
        },
    },
    async mounted() {
        this.provinceCityArea = await provinceCityAreaApi.selectMap()
        this.initHouseProvinceData()
        this.provinceCityArea = await provinceCityAreaApi.selectMap()
        this.operList = await baseOperCompanyApi.selectList()
        this.houseOfficerList = await baseHouseOfficerApi.selectList()
        this.deleteHouseArea()
    },

})